/**
 * Created by liold on 2014/12/15.
 */
var stepAreaTop = $("#stepArea").offset().top;


var stepBox = {
    init: function () {
        var stepBoxHeight = $("#stepBox").outerHeight() + 20;
        $("#stepArea").css("height", stepBoxHeight + "px");

        this.setPosition();

        $(window).scroll(function () {
            stepBox.setPosition();
        });

        $(function () {
            heightMonitor();
        });
    },

    setPosition: function () {
        var windowHeight = $(window).height();
        var scrollHeight = $(window).scrollTop();
        var stepAreaTop = $("#stepArea").offset().top;
        var stepBoxHeight = $("#stepBox").outerHeight() + 2;
        if ($(document).height() > windowHeight) {
            if (windowHeight + scrollHeight > stepAreaTop + stepBoxHeight + 5) {
                //$("#stepBox").removeClass("stepBoxFixed");
                $("#stepBox").css("bottom", (windowHeight + scrollHeight - stepAreaTop - stepBoxHeight - 5) + "px");
            }
            else {
                //$("#stepBox").addClass("stepBoxFixed");
                $("#stepBox").css("bottom", "0px");
            }
        }
        else {
            $("#stepBox").css("bottom", "57px");
        }
    }
};

var lastWindowHeight = $(window).height();
function heightMonitor() {
    var tmpStepAreaTop = $("#stepArea").offset().top;
    var tmpWindowHeight = $(window).height();

    if (tmpStepAreaTop != stepAreaTop || lastWindowHeight != tmpWindowHeight) {
        stepBox.setPosition();
        stepAreaTop = tmpStepAreaTop;
        lastWindowHeight = tmpWindowHeight;
    }


    setTimeout(function () {
        heightMonitor();
    }, 500);
}
